package com.alan.quartz.controller;

import com.alan.quartz.service.QuartzScheduler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by liufan
 * 2018-12-12
 */
@Api("定时任务服务 API")
@RestController
@RequestMapping("/quartz")
public class QuartzController {
    @Autowired
    private QuartzScheduler quartzScheduler;

    @ApiOperation(value = "开始所有", notes = "开始所有")
    @RequestMapping(value = "/start", method = RequestMethod.GET)
    public void startQuartzJob() {
        try {
            quartzScheduler.startJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "获取定时任务信息", notes = "获取定时任务信息")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String getQuartzJob(@ApiParam(value = "定时任务名称") @RequestParam String name, @ApiParam(value = "定时任务分组名称") @RequestParam String group) {
        String info = null;
        try {
            info = quartzScheduler.getJobInfo(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return info;
    }

    @ApiOperation(value = "修改定时任务时间", notes = "修改定时任务时间")
    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public boolean modifyQuartzJob(@ApiParam(value = "定时任务名称") @RequestParam String name, String group, @ApiParam(value = "定时任务运行时间规则（corn表达式）") @RequestParam String time) {
        boolean flag = true;
        try {
            flag = quartzScheduler.modifyJob(name, group, time);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @ApiOperation(value = "暂停某个任务", notes = "暂停某个任务")
    @RequestMapping(value = "/pause", method = RequestMethod.GET)
    public void pauseQuartzJob(@ApiParam(value = "定时任务名称") @RequestParam String name,@ApiParam(value = "定时任务分组名称") @RequestParam String group) {
        try {
            quartzScheduler.pauseJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "暂停所有任务", notes = "暂停所有任务")
    @RequestMapping(value = "/pauseAll", method = RequestMethod.GET)
    public void pauseAllQuartzJob() {
        try {
            quartzScheduler.pauseAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "删除某个任务", notes = "删除某个任务")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public void deleteJob(@ApiParam(value = "定时任务名称") @RequestParam String name,@ApiParam(value = "定时任务分组名称") @RequestParam String group) {
        try {
            quartzScheduler.deleteJob(name, group);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }


}
